<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button v-if="permissions.computed" type="primary" size="default" @click="handleApply()"
            >完工销户申请</el-button
          >
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
          >
            <el-table-column
              label="序号"
              type="index"
              :index="(index: number) => (queryForm.pageNum - 1) * index + 1"
              min-width="50"
            />
            <el-table-column v-if="currentSystem == 'SysUser'" label="项目名称" prop="projectName" min-width="250" />
            <el-table-column v-if="currentSystem == 'SysUser'" label="项目编号" prop="projectNum" min-width="200" />
            <el-table-column label="项目类型" prop="categoryName" min-width="100" />
            <el-table-column label="建设性质" prop="propertyNumName" min-width="80" />
            <el-table-column label="审核状态" prop="auditTypeName" min-width="80">
              <template #default="scope">
                <span v-if="scope.row.auditType == 0">
                  <el-text type="warning"> {{ scope.row.auditTypeName }} </el-text>
                </span>
                <span v-if="scope.row.auditType == 1">
                  <el-text type="success"> {{ scope.row.auditTypeName }} </el-text>
                </span>
                <span v-if="scope.row.auditType == -1">
                  <el-tooltip class="box-item" effect="light" :content="scope.row.refundReason" placement="top">
                    <el-text type="danger"> {{ scope.row.auditTypeName }} </el-text>
                  </el-tooltip>
                </span>
              </template>
            </el-table-column>
            <el-table-column label="申请理由" prop="applyReason" min-width="200" />
            <el-table-column label="申请时间" prop="createTime" min-width="120" />
            <el-table-column label="实际竣工日期" prop="endDate" min-width="120" />
            <el-table-column label="计划竣工日期" prop="contractEndDate" min-width="120" />
            <el-table-column label="操作" fixed="right" max-width="260" :min-width="160">
              <template #default="scope">
                <div v-hasData="scope.row.hasEdit" style="display: inline">
                  <el-button
                    v-if="permissions.auditing && scope.row.auditType == 0"
                    type="primary"
                    link
                    size="small"
                    @click="handleAudit(scope.row)"
                  >
                    审核
                  </el-button>
                  <el-button
                    v-if="permissions.delete && scope.row.auditType == 0"
                    type="primary"
                    link
                    size="small"
                    @click="handelDelete(scope.row)"
                  >
                    删除
                  </el-button>
                  <el-button
                    v-if="permissions.update && scope.row.auditType != 1"
                    type="primary"
                    link
                    size="small"
                    @click="handelUpdate(scope.row)"
                  >
                    修改
                  </el-button>
                </div>
                <el-button
                  v-if="permissions.print && scope.row.auditType == 1 && scope.row.accountId > 0"
                  type="primary"
                  link
                  @click="handelPrint(scope.row)"
                >
                  打印意见
                </el-button>
                <el-button
                  v-if="permissions.detail && scope.row.auditType == 1"
                  type="primary"
                  link
                  size="small"
                  @click="handelDetail(scope.row)"
                >
                  详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageNum"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <print-template v-if="dialogPrint.visible" :dialog-print="dialogPrint" @send-close="sendClose"></print-template>
    <add-form :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
    <detail :dialog-detail="dialogDetail" @send-close="sendClose" />
  </div>
</template>
<script setup lang="ts">
import { ElMessageBox } from "element-plus";
import addForm from "./addForm.vue";
import printTemplate from "./printTemplate.vue";
import detail from "./detail.vue";
import { QueryProjectCompletedList, DeleteProjectCompleted } from "@/api/project/completed/index";
const permissions = ref({
  computed: false,
  detail: false,
  auditing: false,
  delete: false,
  update: false,
  print: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "SysUser") {
  permissions.value.detail = true;
  permissions.value.auditing = true;
}
if (currentSystem.value == "Constructor") {
  permissions.value.detail = true;
}
if (currentSystem.value == "Corp") {
  permissions.value.detail = true;
  if (isLumpContractor) {
    permissions.value.computed = true;
    permissions.value.detail = true;
    permissions.value.update = true;
    permissions.value.delete = true;
    permissions.value.print = true;
  }
}
if (currentSystem.value == "Manager") {
  permissions.value.computed = true;
  permissions.value.detail = true;
  permissions.value.update = true;
  permissions.value.delete = true;
  permissions.value.print = true;
}
if (currentSystem.value == "Commander") {
  permissions.value.detail = true;
}

console.log(isLumpContractor);
console.log(currentSystem);
const loading = ref(false);
const tableData = ref();

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogAudit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const dialogPrint = reactive<any>({
  visible: false,
});
const tableRef = ref();

const handelPrint = (row: any) => {
  //打印意见
  dialogPrint.visible = true;
  dialogPrint.id = row.accountId;
  dialogPrint.title = "打印意见";
  dialogPrint.footBtn = "打印";
};

//新增
const handleApply = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认申请";
  dialogAddOrEdit.title = "完工销户申请";
};

const handelUpdate = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
  dialogAddOrEdit.id = row.id;
};

//审核
const handleAudit = async function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.title = "完工销户审核";
  dialogDetail.footBtn = "确认提交";
  dialogDetail.id = row.id;
  dialogDetail.type = 1;
};
//详情
const handelDetail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
  dialogDetail.type = 0;
};

//删除
const handelDelete = function (row: any) {
  ElMessageBox.confirm("确认删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    loading.value = true;
    const res = await DeleteProjectCompleted(row.id);
    if (res.type === 0) {
      if (res.success) {
        ElMessage.success(res.msg);
        handleQuery();
      } else {
      }
    } else {
    }
    loading.value = false;
  });
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogAudit.visible = false;
  dialogAudit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  dialogPrint.visible = false;
  if (needRefresh) {
    handleQuery();
  }
};

//查询条件
const queryForm = reactive({
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryProjectCompletedList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageNum = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
